<template>
  <view class="content">
    <view class="distribution-box">
      <view class="distribution-nav">
        <view class="distribution-tba">
          <text @click="waimaick(item)">外卖配送</text>
          <text class="active">到店自提</text>
        </view>
        <view class="distribution-tba-box"  @click="dianhua">
          <view class="distribution-diz ziti-b">
            <text>{{ttloagText.store_address}}</text>
          </view>
          <view class="distribution-diz-txt">
            <text>{{ttloagText.store_linkman}}{{ttloagText.store_phone}}</text>
            <text>{{ttloagText.dei}}</text>
          </view>
        </view>
      </view>
      <view class="distribution-shid">
        <view class="distribution-shid-txt">选择到店时间</view>
        <view class="distribution-shid-an" @tap="showBanner2">
          <text>{{timetxt}}</text>
        </view>
      </view>
      <view class="distribution-list-box">
        <view class="distribution-zbt">我点的菜</view>
        <view class="distribution-list" v-for="(gwc,index) in GWClist" :key="index">
          <view class="distribution-list-mc">第{{index + 1}}道菜</view>
          <view class="distribution-list-txt" style="padding-top: 0;">
            <div class="distribution-xfl">
              <view class="cai-si">主菜</view>
              <view v-for="(goods,g) in gwc.detail.goods" :key="g">
                {{goods.title}}+{{goods.attr_name}}
                <text>￥{{goods.discount_price}}</text>
              </view>
            </div>
            <div class="distribution-xfl">
              <view class="cai-si">烹饪方式</view>
              <view>
                {{gwc.detail.cooking.name}}
                <text>￥{{gwc.detail.cooking.price}}</text>
              </view>
              <view class="cai-si">调料</view>
              <view v-for="(seasoning,s) in gwc.detail.seasoning" :key="s">{{seasoning.name}}</view>
              <view class="cai-si">配料</view>
              <view v-for="(collocation,c) in gwc.detail.collocation" :key="c">{{collocation.name}}</view>
							<view class="cai-si">备注：</view>
							<view>{{gwc.remark}}</view>
            </div>
          </view>
        </view>
      </view>
      <view class="distribution-ybg">
        <view>打包费</view>
        <text>￥{{dabao}}</text>
      </view>
      <view class="distribution-psf">
        <view>配送费</view>
        <text>￥{{peis}}</text>
      </view>
    </view>
    <view class="goods-nav">
      <view class="cart-btn-box">
        <view class="heji-b">
          <text>合计</text>
        </view>
        <view class="cart-btn-tetx">
          <text class="cart-cie">￥{{zongjia}}</text>
          <text>已优惠{{youhui}}元</text>
        </view>
      </view>
      <view class="btn-box">
        <button type="default" class="bot-btn2" @click="shouye">首页</button>
        <button type="default" class="bot-btn2" @click="zhifuck(item)">支付</button>
      </view>
    </view>
    <!-- 弹窗提示 -->
    <view class="tc-pop tishi-wid" style="display: none;">
      <div class="tishi-pop">
        <button class="qx-btn">×</button>
        <view class="tshi-box">
          <view class="tshi-box-bt">提示</view>
          <text>请选到店时间</text>
        </view>
      </div>
    </view>
    <!-- 收货地址 -->
    <view class="tc-pop take-wid" v-if="bannerShow">
      <view class="take-pop">
        <view class="take-pop-bt">我的收货地址</view>
        <button class="qx-btn" @tap="closeBanner">×</button>
        <view class="take-box">
          <view class="site-box">
            <view class="site-box-list" v-for="(item,index) in dataList" :key="index">
              <view class="site-box-list-left" @click="chooseAddNum()">
                <view class="site-h3">{{item.dizi}}</view>
                <view>
                  <text>{{item.nane}}</text>
                  <text>{{item.xingb}}</text>
                  <text>{{item.dei}}</text>
                </view>
              </view>
              <view class="site-box-list-right" @click="bianjick()"></view>
            </view>
          </view>
          <view class="add-site">
            <text @click="xzendizi()">新增收货地址</text>
          </view>
        </view>
      </view>
    </view>
    <!-- 时间 -->
    <view class="tc-pop time-wid" v-if="bannerShow2">
      <view class="time-pop">
        <view class="take-pop-bt">选择到店时间</view>
        <button class="qx-btn" @tap="closeBanner">×</button>
        <view class="time-box">
          <view class="time-box-left">
            <text
              :class="[timetabidx == index? 'active' : '']"
              v-for="(item,index) in riqi"
              :key="index"
              @click="timetab(index)"
            >{{item.time}}</text>
          </view>
          <view class="time-box-right">
            <view class="time-shid-list">
              <text
                :class="[currentIndex == index? 'active' : '']"
                v-for="(item,index) in add_class"
                :key="index"
                @click="addClass(index)"
              >{{item.time}}</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 弹窗背景 -->
    <view class="tc-fbg" v-if="bannerShowbg"></view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      data: "",
      GWClist: [],
      zongjia: "",
      youhui: "",
      dabao: 0,
      peis: uni.getStorageSync("jyfw").delivery_cost,
      dbfei: uni.getStorageSync("jyfw").packing_cost,

      currentIndex: -1,
      timetabidx: 0,
      bannerShow: false,
      bannerShowbg: false,
      bannerShow2: false,
      timetxt: "选择时间",
      ttloagText: "",
      dataList: [],
      riqi: [],
      add_class: [
        {
          time: "11:30-12:00"
        },
        {
          time: "12:00-12:30"
        },
        {
          time: "12:30-13:00"
        },
        {
          time: "17:30-18:00"
        },
        {
          time: "18:00-18:30"
        },
        {
          time: "18:30-19:00"
        },
        {
          time: "19:00-19:30"
        }
      ]
    };
  },
  // 监听页面返回
  onBackPress() {
    if (this.bannerShow) {
      this.bannerShow = true;
      return true;
    }
    if (this.bannerShow2) {
      this.bannerShow2 = false;
      return true;
    }
  },
  onLoad() {
    var data = {
      city_id: "", //城市编号
      type: 1, //订单类型(1=派送，2=自提)
      linkman: "", //联系人
      gender: "", //性别
      phone: "", //手机号
      address: "", //地址
      doorplate: "", //门牌号
      lng: "", //经度
      lat: "", //经度
      reservation_time: "", //预约送达时间
      delivery_cost: 0, //配送费
      packing_cost: 0 //打包费
    };
    this.data = data;
    this.ttloagText = uni.getStorageSync("jyfw");
    console.log(this.ttloagText);

    for (let i = 1; i < 4; i++) {
      let curDate = new Date(); // 获取当前时间对象
      const startDate = new Date(curDate.getTime() + i * 24 * 3600 * 1000);
      const startTime =
        startDate.getMonth() + 1 + "月" + startDate.getDate() + "日";

      if (new Date(startDate).getDay() == 0) {
        var xq = "周日";
      } else if (new Date(startDate).getDay() == 1) {
        var xq = "周一";
      } else if (new Date(startDate).getDay() == 2) {
        var xq = "周二";
      } else if (new Date(startDate).getDay() == 3) {
        var xq = "周三";
      } else if (new Date(startDate).getDay() == 4) {
        var xq = "周四";
      } else if (new Date(startDate).getDay() == 5) {
        var xq = "周五";
      } else if (new Date(startDate).getDay() == 6) {
        var xq = "周六";
      }
      this.riqi.push({ time: startTime + " (" + xq + ")" });
      this.data.reservation_time = this.riqi[0].time;
    }
    this.getGWC();
  },
  onShow() {},
  methods: {
    getDizhi() {
      var than = this;
      uni.request({
        url: getApp().globalData.http + "/address/getList",
        data: {},
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          if (res.data.code == 1) {
            than.dataList = res.data.data;
            than.dataList.forEach((e, index) => {
              if (e.is_default == 1) {
                than.ttloagText = e;
                than.chooseAddNum(index);
              }
            });
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            });
          }
        }
      });
    },
    getGWC() {
      //获取购物车列表
      var than = this;
      uni.request({
        url: getApp().globalData.http + "/shoppingCart/getList",
        data: {},
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          than.GWClist = res.data.data;
          var zongqian = 0;
          var yuanjia = 0;
          than.dabao = than.dbfei * than.GWClist.length;
          than.GWClist.forEach(e => {
            e.detail.goods.forEach(i => {
              zongqian += Number(i.discount_price);
              yuanjia += Number(i.market_price);
            });
            zongqian += Number(e.detail.cooking.price);
            yuanjia += Number(e.detail.cooking.price);
          });
          than.zongjia = (zongqian + than.dabao).toFixed(2);
          than.peis = 0;
          than.youhui = (Number(yuanjia) - Number(zongqian)).toFixed(2);
        }
      });
    },
    shouye() {
      wx.reLaunch({
        url: "/pages/index/index"
      });
    },
    timetab(index) {
      this.timetabidx = index;
    },
    bindtapFunc: function(e) {},
    closeBanner: function() {
      this.bannerShow = false;
      this.bannerShow2 = false;
      this.bannerShowbg = false;
    },
    showBanner: function() {
      this.bannerShow = true;
      this.bannerShowbg = true;
    },
    showBanner2: function() {
      this.bannerShow2 = true;
      this.bannerShowbg = true;
    },
    //地址选择
    chooseAddNum: function(e) {
      this.ttloagText.dizi = this.dataList[0].dizi;
      this.ttloagText.nane = this.dataList[0].nane;
      this.ttloagText.xingb = this.dataList[0].xingb;
      this.ttloagText.dei = this.dataList[0].dei;
      this.bannerShow = false;
      this.bannerShowbg = false;
    },
    //时间选择
    addClass(index) {
      this.currentIndex = index;
      this.timetxt =
        this.riqi[this.timetabidx].time + this.add_class[index].time;
      this.data.reservation_time = this.timetxt;
      this.bannerShow2 = false;
      this.bannerShowbg = false;
    },
    //新增地址
    xzendizi() {
      wx.navigateTo({
        url: "/pages/my/xsite-edit"
      });
    },
    dianhua() {
      wx.makePhoneCall({
        phoneNumber: uni.getStorageSync("jyfw").store_phone
      });
    },
    //提交
    waimaick() {
      wx.navigateBack({
        delta: 1
      });
    },
    //支付
    zhifuck() {
      if (this.timetxt == "选择时间") {
        wx.showToast({
          title: "请选择到店时间。",
          icon: "none",
          duration: 2000
        });
        return;
      }
      if (uni.getStorageSync("user").nickname == null) {
        wx.navigateTo({
          url: "/pages/my/login"
        });
        return
      }
      var num = 0;
      this.GWClist.forEach(e => {
        num += e.num;
      });

      var than = this;
      uni.request({
        url: getApp().globalData.http + "/order/create",
        data: {
          city_id: "", //城市编号
          type: 2, //订单类型(1=派送，2=自提)
          linkman: "", //联系人
          gender: "", //性别
          phone: "", //手机号
          address: "", //地址
          doorplate: "", //门牌号
          lng: "", //经度
          lat: "", //经度
          reservation_time: this.data.reservation_time, //预约送达时间
          delivery_cost: 0, //配送费
          packing_cost: uni.getStorageSync("jyfw").packing_cost * num //打包费
        },
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: data => {
          if (data.data.code == 1) {
            uni.request({
              url: getApp().globalData.http + "/order/pay",
              data: {
                id: data.data.data.id
              },
              method: "post",
              header: {
                "content-type": "application/x-www-form-urlencoded",
                token: uni.getStorageSync("token")
              },
              success: res => {
                if (res.data.code) {
                  wx.requestPayment({
                    timeStamp: res.data.data.timeStamp,
                    nonceStr: res.data.data.nonceStr,
                    package: res.data.data.package,
                    signType: res.data.data.signType,
                    paySign: res.data.data.paySign,
                    success(e) {
                      wx.reLaunch({
                        url: "/pages/my/cfzhifu?id=" + data.data.data.id
                      });
                    },
                    fail(res) {
                      wx.reLaunch({
                        url: "/pages/my/order"
                      });
                    }
                  });
                } else {
                  wx.showToast({
                    title: res.data.msg,
                    icon: "none",
                    duration: 2000
                  });
                }
              }
            });
          } else {
            wx.showToast({
              title: data.data.msg,
              icon: "none",
              duration: 2000
            });
          }
        }
      });
    }
  }
};
</script>